package com.example.taobaounion.ui.activity;

import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseActivity;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.ui.fragment.HomeFragment;
import com.example.taobaounion.ui.fragment.OnSellFragment;
import com.example.taobaounion.ui.fragment.SearchFragment;
import com.example.taobaounion.ui.fragment.SelectedFragment;
import com.example.taobaounion.utils.LogUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements IMainActivity{
    //使用黄油刀
    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;

    private static String TAG="MainActivity";
    private HomeFragment mHomeFragment;
    private OnSellFragment mOnSellFragment;
    private SearchFragment mSearchFragment;
    private SelectedFragment mSelectedFragment;
    private FragmentManager mFm;
    private FragmentTransaction mTransaction;


    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initEvent() {
        initListener();
    }

    @Override
    protected void initView() {
        initFragments();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }


    private void initFragments() {
        mHomeFragment = new HomeFragment();
        mOnSellFragment = new OnSellFragment();
        mSearchFragment = new SearchFragment();
        mSelectedFragment = new SelectedFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mHomeFragment);
    }

    private void initListener() {
       mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
           @Override
           public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
               if(item.getItemId()==R.id.home){
                   LogUtils.d(MainActivity.this, "切换到 "+item.getTitle());
                   switchFragment (mHomeFragment);
               }else if(item.getItemId()==R.id.search){
                   LogUtils.i(MainActivity.this,"切换到 "+item.getTitle());
                   switchFragment (mSearchFragment);
               }else if(item.getItemId()==R.id.red_packet){
                   LogUtils.w(MainActivity.this, "切换到 "+item.getTitle());
                   switchFragment (mOnSellFragment);
               }else if(item.getItemId()==R.id.selected){
                   LogUtils.e(MainActivity.this, "切换到 "+item.getTitle());
                   switchFragment (mSelectedFragment);
               }
               //返回true才有效果，表示我们消费使用了这个事件,终结事件传递,不然程序不给结果
               return true;
           }
       });

         
    }
    //上一次显示的Fragment
    private BaseFragment  lastOneFragment=null;

    private void switchFragment(BaseFragment fragment) {
        //每次切换一次fragment都需要开启一次开启事务和提交事务，不然会报错
        //开启事务
        //修改成add和hide的方式来控制Fragment的切换（避免每次切换都要重新加载界面）
        mTransaction = mFm.beginTransaction();
        if(!fragment.isAdded()){
            mTransaction.add(R.id.main_page_container,fragment);//只增加的话如果不隐藏的话所有fragment会出现在同一个界面
        }else {
            mTransaction.show(fragment);
        }
        if(lastOneFragment!=null&&lastOneFragment!=fragment){//避免重复点击一个导航栏会进行隐藏
            mTransaction.hide(lastOneFragment);
        }
        lastOneFragment=fragment;

        //切换fragment
        //mTransaction.replace(R.id.main_page_container,fragment);
        //提交事务(不提交无法看到Fragment)
        mTransaction.commit();
    }

//    public String getText(){
//        return "hello world....";
//    }

    /**
     * 跳转到搜索界面
     */
    public void switch2SearchPage(){
        //switchFragment(mSearchFragment);
        //切换导航栏的选中项
        mNavigationView.setSelectedItemId(R.id.search);
    }

/*    private void initView() {
        //mNavigationView = findViewById(R.id.main_navigation_bar);
        HomeFragment homeFragment = new HomeFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.main_page_container,homeFragment);
        transaction.commit();
    }*/
}